package com.srwl.mytx.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.srwl.mytx.Constant;
import com.srwl.mytx.Helper;
import com.srwl.mytx.MyApplication;
import com.srwl.mytx.R;
import com.srwl.mytx.RetrofitManager;
import com.srwl.mytx.activity.ChatRoomActivity;
import com.srwl.mytx.domain.AliOssService;
import com.srwl.mytx.domain.CommonResult;
import com.srwl.mytx.domain.LiveRoom;
import com.srwl.mytx.domain.Topic;
import com.srwl.mytx.service.CommentService;
import com.srwl.mytx.utils.ClickUtil;
import com.srwl.mytx.utils.DateUtils;
import com.srwl.mytx.utils.GlideLoadUtils;
import com.srwl.mytx.utils.Utils;

import java.util.LinkedHashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jzvd.JZDataSource;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MapBottomTopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "HomePageTopicAdapter";
    private final List<Topic> topicList;
    private final Context context;
    private boolean bol_noData;//标记是否有数据
    private boolean bol_requestError;//标记是否请求错误
    private static int ITEM_VIDEO_TOPIC = 5;
    private CallbackToOutside callbackToOutside;


    public MapBottomTopicAdapter(Context context, List<Topic> topicList) {
        this.topicList = topicList;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        RecyclerView.ViewHolder holder;
        if (viewType == Constant.ITEM_TYPE_NO_DATA) {
            holder = new NoDataViewHolder(LayoutInflater.from(context).inflate(R.layout.no__data__layout, viewGroup, false));
        } else if (viewType == Constant.ITEM_TYPE_REQUEST_ERROR) {
            holder = new RequestErrorViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.request_error_layout, viewGroup, false));
        } else if (viewType == ITEM_VIDEO_TOPIC) {
            holder = new VideoTopicHolder(LayoutInflater.from(context).inflate(R.layout.topic_list_video_item_for_map, viewGroup, false));
        } else {
            holder = new PhotoTopicHolder(LayoutInflater.from(context).inflate(R.layout.topic_list_photo_item_for_map, viewGroup, false));
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int viewType = getItemViewType(position);
        if (viewType == Constant.ITEM_TYPE_NO_DATA) {
            NoDataViewHolder noDataViewHolder = (NoDataViewHolder) holder;
            noDataViewHolder.tv_tips.setText("没有数据");
        } else if (viewType == Constant.ITEM_TYPE_REQUEST_ERROR) {
            RequestErrorViewHolder errorHolder = (RequestErrorViewHolder) holder;
//            errorHolder.tv_tips.setText("无网络");
        } else if (viewType == ITEM_VIDEO_TOPIC) {
            //视频主题
            Topic topic = topicList.get(position);
            VideoTopicHolder viewHolder = (VideoTopicHolder) holder;
            viewHolder.tv_title.setText(topic.getTitle());
            if (TextUtils.isEmpty(topic.getContent())) {
                viewHolder.tv_content.setVisibility(View.GONE);
            } else {
                viewHolder.tv_content.setText(topic.getContent());
            }
            Integer type = topic.getType();
            viewHolder.iv_title_head.setVisibility(View.VISIBLE);
            if (type == Constant.TOPIC_TYPE_EXPOSE) {
                viewHolder.iv_liveRoom.setVisibility(View.GONE);
                viewHolder.iv_title_head.setImageResource(R.drawable.tx_expose);
                viewHolder.rl_rewardIcon.setVisibility(View.GONE);
                viewHolder.iv_reward.setVisibility(View.INVISIBLE);
                viewHolder.tv_reward.setVisibility(View.INVISIBLE);
            } else if (type == Constant.TOPIC_TYPE_REWARD) {
                viewHolder.iv_liveRoom.setVisibility(View.GONE);
                viewHolder.iv_title_head.setImageResource(R.drawable.tx_reward_1);
                viewHolder.rl_rewardIcon.setVisibility(View.VISIBLE);
                viewHolder.iv_reward.setVisibility(View.VISIBLE);
                viewHolder.tv_reward.setVisibility(View.VISIBLE);
                viewHolder.tv_reward.setText(Utils.moneyFormat(topic.getPrice()));
            } else if (type == Constant.TOPIC_TYPE_AREA) {
                viewHolder.iv_liveRoom.setVisibility(View.VISIBLE);
                viewHolder.iv_title_head.setImageResource(R.drawable.tx_topic_head_area_1);
                viewHolder.rl_rewardIcon.setVisibility(View.GONE);
                viewHolder.iv_reward.setVisibility(View.INVISIBLE);
                viewHolder.tv_reward.setVisibility(View.INVISIBLE);
            } else if (type == Constant.TOPIC_TYPE_INDUSTRY) {
                viewHolder.iv_liveRoom.setVisibility(View.VISIBLE);
                viewHolder.iv_title_head.setImageResource(R.drawable.tx_topic_head_indeustry_1);
                viewHolder.rl_rewardIcon.setVisibility(View.GONE);
                viewHolder.iv_reward.setVisibility(View.INVISIBLE);
                viewHolder.tv_reward.setVisibility(View.INVISIBLE);
            } else if (type == Constant.TOPIC_TYPE_SCHOOL) {
                viewHolder.iv_liveRoom.setVisibility(View.VISIBLE);
                viewHolder.iv_title_head.setImageResource(R.drawable.tx_topic_head_school);
                viewHolder.rl_rewardIcon.setVisibility(View.GONE);
                viewHolder.iv_reward.setVisibility(View.INVISIBLE);
                viewHolder.tv_reward.setVisibility(View.INVISIBLE);
            } else {
                viewHolder.iv_liveRoom.setVisibility(View.GONE);
                viewHolder.iv_title_head.setVisibility(View.GONE);
            }

            viewHolder.tv_address.setText(topic.getAddress());
            viewHolder.videoPlayer.batteryLevel.setVisibility(View.GONE);
            viewHolder.videoPlayer.backButton.setVisibility(View.GONE);


            LinkedHashMap map = new LinkedHashMap();
            String url = topic.getVideo();
            String remoteUrl;
            if (url != null && url.startsWith("http")) {
                remoteUrl = url;
            } else {
                remoteUrl = AliOssService.TOPIC_VIDEO_PRE_URL + url;
            }
            JzvdStd.setVideoImageDisplayType(Jzvd.VIDEO_IMAGE_DISPLAY_TYPE_ORIGINAL);
            String proxyUrl = MyApplication.getProxy(context).getProxyUrl(remoteUrl);//视频缓存代理
            map.put("高清", proxyUrl);
            JZDataSource jzDataSource = new JZDataSource(map, "");
            jzDataSource.looping = true;
            // jzDataSource.currentUrlIndex = 2;
            viewHolder.videoPlayer.setUp(jzDataSource, JzvdStd.SCREEN_TINY);
            viewHolder.videoPlayer.fullscreenButton.setVisibility(View.INVISIBLE);
            viewHolder.videoPlayer.tinyBackImageView.setVisibility(View.INVISIBLE);
            // videoPlayer.bottomContainer.setVisibility(View.VISIBLE);
            viewHolder.videoPlayer.thumbImageView.setVisibility(View.VISIBLE);
            viewHolder.videoPlayer.startButton.setVisibility(View.VISIBLE);
            viewHolder.videoPlayer.thumbImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            viewHolder.videoPlayer.thumbImageView.setOnClickListener(v -> {
                if (callbackToOutside != null) {
                    //将跳转activity的方法回调到fragment中执行
                    callbackToOutside.jumpPage(topic);
                }
            });
            //当播放器处理暂停状态，点击视频的回调
            viewHolder.videoPlayer.setSurfaceViewClick(() -> {
                if (callbackToOutside != null) {
                    //将跳转activity的方法回调到fragment中执行
                    callbackToOutside.jumpPage(topic);
                }
            });
            String coverUrl = Utils.getCoverUrl(topic, "");
            // String coverUrl = Utils.getTopicListPreviewUrl(topic); //尝试使用压缩50的策略，但是封面图片噪点严重，封面图片本身质量不是很好了，
            //Glide.with(context).load(coverUrl).into(viewHolder.videoPlayer.thumbImageView);
            GlideLoadUtils.glideLoad(context, coverUrl, viewHolder.videoPlayer.thumbImageView);

            if (topic.getUpdated() != null) {
                viewHolder.tv_update_date.setText(DateUtils.getDateToString(topic.getUpdated()));
            } else {
                viewHolder.tv_update_date.setText(DateUtils.getDateToString(topic.getCreated()));
            }


            //加载收藏 评论 点赞 踩的数量
            // TODO: 2019/4/17  收藏数量
            //viewHolder.tv_collection_num.setText("还未创建改字段");
            viewHolder.tv_comment_num.setText(Utils.numberFormat2(topic.getCommentSize()));
            viewHolder.tv_like_num.setText(Utils.numberFormat2(topic.getPraiseSize()));

            //动态加载点赞图标
            changeLikeImage(((VideoTopicHolder) holder).iv_like, topic.getHadPraise());


            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (ClickUtil.isFastClick()) return;

                    if (callbackToOutside != null) {
                        //将跳转activity的方法回调到fragment中执行
                        callbackToOutside.jumpPage(topic);
                    }
                }
            });

            //给点赞图标注册点击事件
            viewHolder.tv_like.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (ClickUtil.isFastClick()) return;
                    if (!Helper.getInstance().isNetworkConnected()) {
                        Toast.makeText(context, context.getString(R.string.network_anomalies), Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (topic.getHadPraise()) {
                        //已经点赞，撤销点赞
                        removeTopicPraise(holder, topic);
                        ((VideoTopicHolder) holder).iv_like.setImageResource(R.drawable.praise);
                    } else {
                        //还未点赞，增加点赞
                        addTopicPraise(holder, topic);
                        ((VideoTopicHolder) holder).iv_like.setImageResource(R.drawable.praise1);
                    }
                }
            });

            //给评论图标注册点击事件
            viewHolder.tv_comment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //跳转详情页面，并打开评论视图
                    if (ClickUtil.isFastClick()) return;

                    if (callbackToOutside != null) {
                        callbackToOutside.commentJumpPage(topic);
                    }

                }
            });

            //给电视机图标注册点击事件
            viewHolder.iv_liveRoom.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //组合聊天室数据
                    LiveRoom liveRoom = new LiveRoom();
                    liveRoom.setId(topic.getId());
                    liveRoom.setManagerId(topic.getManagerId());
                    liveRoom.setChatId(topic.getId());
                    liveRoom.setTitle(topic.getTitle());
                    liveRoom.setStatus(topic.getStatus());
                    liveRoom.setVideo(topic.getVideo());
                    if (topic.getPictures() != null && topic.getPictures().size() != 0) {//图片和视频，至少有一样存在
                        liveRoom.setCover(topic.getPictures().get(0));
                    } else {
                        liveRoom.setCover(topic.getVideoCover());
                    }
                    //跳转到当前主题的聊天室activity
                    context.startActivity(new Intent(context, ChatRoomActivity.class).putExtra(Constant.EXTRA_INFO_LIVE_ROOM, liveRoom));
                }
            });

        } else {
            //图片主题
            Topic topic = topicList.get(position);
            PhotoTopicHolder viewHolder = (PhotoTopicHolder) holder;
            viewHolder.tv_title.setText(topic.getTitle());
            if (TextUtils.isEmpty(topic.getContent())) {
                viewHolder.tv_content.setVisibility(View.GONE);
            } else {
                viewHolder.tv_content.setText(topic.getContent());
            }
            Integer type = topic.getType();
            viewHolder.iv_title_head.setVisibility(View.VISIBLE);
            if (type == Constant.TOPIC_TYPE_EXPOSE) {
                viewHolder.iv_liveRoom.setVisibility(View.GONE);
                viewHolder.iv_title_head.setImageResource(R.drawable.tx_expose);
                viewHolder.rl_rewardIcon.setVisibility(View.GONE);
                viewHolder.iv_reward.setVisibility(View.INVISIBLE);
                viewHolder.tv_reward.setVisibility(View.INVISIBLE);
            } else if (type == Constant.TOPIC_TYPE_REWARD) {
                viewHolder.iv_liveRoom.setVisibility(View.GONE);
                viewHolder.iv_title_head.setImageResource(R.drawable.tx_reward_1);
                viewHolder.rl_rewardIcon.setVisibility(View.VISIBLE);
                viewHolder.iv_reward.setVisibility(View.VISIBLE);
                viewHolder.tv_reward.setVisibility(View.VISIBLE);
                viewHolder.tv_reward.setText(Utils.moneyFormat(topic.getPrice()));
                //viewHolder.tv_reward.setText(topic.getReward() + "");
            } else if (type == Constant.TOPIC_TYPE_AREA) {
                viewHolder.iv_liveRoom.setVisibility(View.VISIBLE);
                viewHolder.iv_title_head.setImageResource(R.drawable.tx_topic_head_area_1);
                viewHolder.rl_rewardIcon.setVisibility(View.GONE);
                viewHolder.iv_reward.setVisibility(View.INVISIBLE);
                viewHolder.tv_reward.setVisibility(View.INVISIBLE);
            } else if (type == Constant.TOPIC_TYPE_INDUSTRY) {
                viewHolder.iv_liveRoom.setVisibility(View.VISIBLE);
                viewHolder.iv_title_head.setImageResource(R.drawable.tx_topic_head_indeustry_1);
                viewHolder.rl_rewardIcon.setVisibility(View.GONE);
                viewHolder.iv_reward.setVisibility(View.INVISIBLE);
                viewHolder.tv_reward.setVisibility(View.INVISIBLE);
            } else if (type == Constant.TOPIC_TYPE_SCHOOL) {
                viewHolder.iv_liveRoom.setVisibility(View.VISIBLE);
                viewHolder.iv_title_head.setImageResource(R.drawable.tx_topic_head_school);
                viewHolder.rl_rewardIcon.setVisibility(View.GONE);
                viewHolder.iv_reward.setVisibility(View.INVISIBLE);
                viewHolder.tv_reward.setVisibility(View.INVISIBLE);
            } else {
                viewHolder.iv_liveRoom.setVisibility(View.VISIBLE);
                viewHolder.iv_title_head.setVisibility(View.GONE);
            }

            viewHolder.tv_address.setText(topic.getAddress());
            {
                String url = topic.getPictures().get(0);
                // String remoteUrl = generateFullUrl(url);
                String remoteUrl = Utils.getPictureUrlScale50(url);
                // Glide.with(context).load(remoteUrl).into(viewHolder.iv_preview_01);
                GlideLoadUtils.glideLoad(context, remoteUrl, viewHolder.iv_preview_01);
            }
            viewHolder.iv_preview_02.setVisibility(View.VISIBLE);
            viewHolder.iv_preview_03.setVisibility(View.VISIBLE);
            if (topic.getPictures().size() > 1) {
                String url = topic.getPictures().get(1);
                //String remoteUrl = generateFullUrl(url);
                String remoteUrl = Utils.getPictureUrlScale50(url);
                // Glide.with(context).load(remoteUrl).into(viewHolder.iv_preview_02);
                GlideLoadUtils.glideLoad(context, remoteUrl, viewHolder.iv_preview_02);
            } else {
                viewHolder.iv_preview_02.setVisibility(View.GONE);
            }

            if (topic.getPictures().size() > 2) {
                String url = topic.getPictures().get(2);
                // String remoteUrl = generateFullUrl(url);
                String remoteUrl = Utils.getPictureUrlScale50(url);
                //Glide.with(context).load(remoteUrl).into(viewHolder.iv_preview_03);
                GlideLoadUtils.glideLoad(context, remoteUrl, viewHolder.iv_preview_03);
            } else {
                viewHolder.iv_preview_03.setVisibility(View.GONE);
            }
            viewHolder.tv_comment_num.setText(Utils.numberFormat2(topic.getCommentSize()));
            viewHolder.tv_like_num.setText(Utils.numberFormat2(topic.getPraiseSize()));

            if (topic.getUpdated() != null) {
                viewHolder.tv_update_date.setText(DateUtils.getDateToString(topic.getUpdated()));
            } else {
                viewHolder.tv_update_date.setText(DateUtils.getDateToString(topic.getCreated()));
            }

            changeLikeImage(((PhotoTopicHolder) holder).iv_like, topic.getHadPraise());  //动态加载点赞图标

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (ClickUtil.isFastClick()) return;

                    if (callbackToOutside != null) {
                        //回调到外面处理页面跳转
                        callbackToOutside.jumpPage(topic);
                    }
                    //context.startActivity(new Intent(context, TopicDetailsActivity.class).putExtra(Constant.EXTRA_INFO_TOPIC, topic));
                    //((Activity) context).startActivityForResult(new Intent(context, TopicDetailsActivity.class).putExtra(Constant.EXTRA_INFO_TOPIC, topic), GaoDeMapFragment.REQUEST_CODE_TO_TOPIC_DETAIL);
                }
            });


            //给点赞图标注册点击事件
            viewHolder.tv_like.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (ClickUtil.isFastClick()) return;    //防止快速点击带来的数据不安全
                    if (topic.getHadPraise()) {
                        //已经点赞，撤销点赞
                        removeTopicPraise(holder, topic);
                        ((PhotoTopicHolder) holder).iv_like.setImageResource(R.drawable.praise);
                    } else {
                        //还未点赞，增加点赞
                        addTopicPraise(holder, topic);
                        ((PhotoTopicHolder) holder).iv_like.setImageResource(R.drawable.praise1);
                    }
                }
            });

            //给评论图标注册点击事件
            viewHolder.tv_comment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //跳转详情页面，并打开评论视图
                    if (ClickUtil.isFastClick()) return;
                    if (callbackToOutside != null) {
                        callbackToOutside.commentJumpPage(topic);
                    }
                }
            });

            //给电视机图标注册点击事件
            viewHolder.iv_liveRoom.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //组合聊天室数据
                    LiveRoom liveRoom = new LiveRoom();
                    liveRoom.setId(topic.getId());
                    liveRoom.setManagerId(topic.getManagerId());
                    liveRoom.setChatId(topic.getId());
                    liveRoom.setTitle(topic.getTitle());
                    liveRoom.setStatus(topic.getStatus());
                    liveRoom.setVideo(topic.getVideo());
                    if (topic.getPictures() != null && topic.getPictures().size() != 0) {//图片和视频，至少有一样存在
                        liveRoom.setCover(topic.getPictures().get(0));
                    } else {
                        liveRoom.setCover(topic.getVideoCover());
                    }
                    //跳转到当前主题的聊天室活动中
                    context.startActivity(new Intent(context, ChatRoomActivity.class).putExtra(Constant.EXTRA_INFO_LIVE_ROOM, liveRoom));
                }
            });

        }

    }

    /**
     * 生成图片请求地址
     *
     * @param url
     * @return
     */
    private String generateFullUrl(String url) {
        String remoteUrl;
        if (url != null && url.startsWith("http")) {
            remoteUrl = url;
        } else {
            remoteUrl = AliOssService.TOPIC_IMAGE_PRE_URL + url;
        }
        return remoteUrl;
    }


    @Override
    public int getItemCount() {
        if (bol_noData || bol_requestError) {
            return 1;
        }
        return topicList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (bol_noData) {
            return Constant.ITEM_TYPE_NO_DATA;
        } else if (bol_requestError) {
            return Constant.ITEM_TYPE_REQUEST_ERROR;
        } else if (!TextUtils.isEmpty(topicList.get(position).getVideo())) {
            return ITEM_VIDEO_TOPIC;
        }
        return Constant.ITEM_TYPE_NORMAL;
    }

    class VideoTopicHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_title)
        TextView tv_title;
        @BindView(R.id.iv_reward)
        ImageView iv_reward;
        @BindView(R.id.tv_money_amount)
        TextView tv_reward;
        @BindView(R.id.iv_title_head)
        ImageView iv_title_head;
        @BindView(R.id.rl_reward_icon)
        LinearLayout rl_rewardIcon;
        @BindView(R.id.tv_address)
        TextView tv_address;
        @BindView(R.id.video_player)
        JzvdStd videoPlayer;

        @BindView(R.id.tv_comment_num)
        TextView tv_comment_num;
        @BindView(R.id.tv_like_num)
        TextView tv_like_num;
        @BindView(R.id.tv_dislike_num)
        TextView tv_dislike_num;
        @BindView(R.id.tv_update_date)
        TextView tv_update_date;
        @BindView(R.id.iv_like)
        ImageView iv_like;
        @BindView(R.id.tv_like)
        TextView tv_like;
        @BindView(R.id.iv_dislike)
        ImageView iv_dislike;
        @BindView(R.id.tv_content)
        TextView tv_content;
        @BindView(R.id.tv_comment)
        TextView tv_comment;
        @BindView(R.id.iv_live_room)
        ImageView iv_liveRoom;


        public VideoTopicHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }

    class PhotoTopicHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_title)
        TextView tv_title;
        @BindView(R.id.iv_reward)
        ImageView iv_reward;
        @BindView(R.id.tv_money_amount)
        TextView tv_reward;
        @BindView(R.id.iv_title_head)
        ImageView iv_title_head;
        @BindView(R.id.rl_reward_icon)
        LinearLayout rl_rewardIcon;
        @BindView(R.id.tv_address)
        TextView tv_address;
        @BindView(R.id.iv_preview_01)
        ImageView iv_preview_01;
        @BindView(R.id.iv_preview_02)
        ImageView iv_preview_02;
        @BindView(R.id.iv_preview_03)
        ImageView iv_preview_03;
        @BindView(R.id.tv_comment_num)
        TextView tv_comment_num;
        @BindView(R.id.tv_like_num)
        TextView tv_like_num;
        @BindView(R.id.tv_dislike_num)
        TextView tv_dislike_num;
        @BindView(R.id.tv_update_date)
        TextView tv_update_date;
        @BindView(R.id.iv_like)
        ImageView iv_like;
        @BindView(R.id.tv_like)
        TextView tv_like;
        @BindView(R.id.iv_dislike)
        ImageView iv_dislike;
        @BindView(R.id.tv_content)
        TextView tv_content;
        @BindView(R.id.tv_comment)
        TextView tv_comment;
        @BindView(R.id.iv_live_room)
        ImageView iv_liveRoom;


        public PhotoTopicHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }


    /**
     * 显示普通视图
     * 有数据的时候调用
     */
    public void showNormalView(int positionStart, int itemCount) {
        bol_noData = false;
        bol_requestError = false;
        notifyItemRangeChanged(positionStart, itemCount);
    }

    public void showNormalView() {
        bol_noData = false;
        bol_requestError = false;
        notifyDataSetChanged();
    }

    /**
     * 显示无数据视图
     */
    public void showNoDataView() {
        bol_noData = true;
        notifyDataSetChanged();
    }

    /**
     * 显示请求数据错误视图
     */
    public void showErrorView() {
        bol_requestError = true;
        notifyDataSetChanged();
    }

    /*
     点赞
     */
    private void addTopicPraise(RecyclerView.ViewHolder holder, Topic topic) {
        topic.setHadPraise(true);
        Integer praiseSize = topic.getPraiseSize();
        if (praiseSize == null) praiseSize = 0;  //当该主题的点赞数为null，说明还没有被点赞过，将其点赞数赋值为0
        topic.setPraiseSize(++praiseSize);
        if (holder instanceof PhotoTopicHolder) {
            //同步前端页面上显示的点赞数
            ((PhotoTopicHolder) holder).tv_like_num.setText(Utils.numberFormat2(praiseSize));
            //同步后端redis中储存的点赞数
            addTopicPraiseServer(topic.getId());

        } else if (holder instanceof VideoTopicHolder) {
            ((VideoTopicHolder) holder).tv_like_num.setText(Utils.numberFormat2(praiseSize));
            addTopicPraiseServer(topic.getId());
        }

    }

    /*
     撤销点赞
     */
    private void removeTopicPraise(RecyclerView.ViewHolder holder, Topic topic) {
        topic.setHadPraise(false);
        Integer praiseSize = topic.getPraiseSize();
        topic.setPraiseSize(--praiseSize);
        if (holder instanceof PhotoTopicHolder) {
            ((PhotoTopicHolder) holder).tv_like_num.setText(Utils.numberFormat2(praiseSize));
            removeTopicPraiseServer(topic.getId());
        } else if (holder instanceof VideoTopicHolder) {
            ((VideoTopicHolder) holder).tv_like_num.setText(Utils.numberFormat2(praiseSize));
            removeTopicPraiseServer(topic.getId());
        }
    }

    /*
     发送请求，后端点赞记录增加1
     */
    private void addTopicPraiseServer(String topicId) {
        RetrofitManager.getInstance()
                .createRequest(CommentService.class)
                .addTopicPraise(topicId)
                .enqueue(new Callback<CommonResult>() {
                    @Override
                    public void onResponse(Call<CommonResult> call, Response<CommonResult> response) {
                        CommonResult result = response.body();
                        if (!response.isSuccessful() || result == null) {
                            Log.e(TAG, "onResponse: " + response.message());
                            return;
                        }
                        if (result.isError()) {
                            Log.e(TAG, result.getMsg());
                            return;
                        }
                    }

                    @Override
                    public void onFailure(Call<CommonResult> call, Throwable t) {
                        t.printStackTrace();
                        return;
                    }
                });
    }

    /*
     发送请求，后端点赞记录减少1
     */
    private void removeTopicPraiseServer(String topicId) {
        RetrofitManager.getInstance()
                .createRequest(CommentService.class)
                .removeTopicPraise(topicId)
                .enqueue(new Callback<CommonResult>() {
                    @Override
                    public void onResponse(Call<CommonResult> call, Response<CommonResult> response) {
                        CommonResult result = response.body();
                        if (!response.isSuccessful() || result == null) {
                            Log.e(TAG, "onResponse: " + response.message());
                            return;
                        }
                        if (result.isError()) {
                            Log.e(TAG, result.getMsg());
                            return;
                        }
                    }

                    @Override
                    public void onFailure(Call<CommonResult> call, Throwable t) {
                        t.printStackTrace();
                        return;
                    }
                });
    }


    /*
    在加载item时根据是否点赞来改变点赞图标的显示
     */
    private void changeLikeImage(ImageView iv_like, boolean hadPraise) {
        if (hadPraise) {
            iv_like.setImageResource(R.drawable.praise1);
        } else {
            iv_like.setImageResource(R.drawable.praise);
        }
    }


    /*
      回调接口
     */
    public interface CallbackToOutside {
        //跳转主题详情页面
        void jumpPage(Topic topic);

        //携带"评论"标示的跳转主题详情页面。表示是点击评论图标跳转到主题详情的
        void commentJumpPage(Topic topic);
    }

    public void setCallbackToOutside(CallbackToOutside callbackToOutside) {
        this.callbackToOutside = callbackToOutside;
    }

}
